﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class PayModeController : BaseController<usersorder>
    {
        //
        // GET: /PayMode/   
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }
        public override BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }

        }
        public class ModeOfPay
        {
            public string payType;//支付方式
            public decimal money;//对应的金额
            public string storeName;
            public string orderTime;
        }
        List<ModeOfPay> listt = new List<ModeOfPay>();

        StoreBll storeBll = new StoreBll();

        /// <summary>
        /// 获取店铺列表
        /// </summary>
        /// <returns></returns>  

        public ActionResult GetShopName()
        {
            //var storeList = storeBll.Search().Select(x => new { shopID = x.storeid, shopName = x.storeName }).ToList();
            return Json(storeBll.Search(x=>x.storeStatus==1).Select(x => new { shopID = x.storeid, shopName = x.storeName }).ToList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetOrderAll(string storeid, string date1, string date2)
        {
            Expression<Func<usersorder, bool>> condition = null;
            //默认今天 所有店铺
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid == null)
            {
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                condition = x => x.orderStatus == 1 && x.orderTime <= todayNow && x.orderTime >= midnight;
            }

            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != null)
            {
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                int storeID = Convert.ToInt32(storeid);
                condition = x => x.orderStatus == 1 && x.orderTime <= todayNow && x.orderTime >= midnight && x.storeid == storeID;
            }
            if (date1 != null && date1 != "" && date2 != null && date2 != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
            }
            if (date1 != "" && date2 != "" && storeid != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                int storeID = Convert.ToInt32(storeid);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID;
            }
            //查询该时间段的订单
            var listOrderTest = bll.Search().Where(condition).Select(x => new { }).ToList();
            var listOrder = bll.Search().Where(condition).GroupBy(x=>x.orderPay).Select(x => new { orderSumprice=x.Sum(y=>y.orderSumprice), x.FirstOrDefault().storeid,orderPay = x.FirstOrDefault().orderPay == 1 ? "支付宝" : x.FirstOrDefault().orderPay == 2 ? "微信" : x.FirstOrDefault().orderPay == 3 ? "优惠券" : "人脸", }).ToList();
            return Json(new { rows = listOrder }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetChart()
        {
            Expression<Func<usersorder, bool>> condition1 = null;
            DateTime todayNow = DateTime.Now;
            DateTime midnight = DateTime.Now;
            midnight = midnight.Date;
            condition1 = x => x.orderStatus == 1 && x.orderTime <= todayNow && x.orderTime >= midnight;
            var listOrderCh = bll.Search().Where(condition1).GroupBy(x => x.orderPay).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, orderPay = x.FirstOrDefault().orderPay == 1 ? "支付宝" : x.FirstOrDefault().orderPay == 2 ? "微信" : x.FirstOrDefault().orderPay == 3 ? "优惠券" : "人脸", }).ToList();
            foreach(var item in listOrderCh )
            {
                ModeOfPay model = new ModeOfPay();
                model.payType = item.orderPay;
                model.money = item.orderSumprice;
                listt.Add(model);
            }

             return Json(listt, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetChartsByDate(string storeid, string date1, string date2)
        {
            Expression<Func<usersorder, bool>> condition = null;
            //默认今天 所有店铺
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid == null)
            {
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                condition = x => x.orderStatus == 1 && x.orderTime <= todayNow && x.orderTime >= midnight;
            }

            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != null)
            {
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                int storeID = Convert.ToInt32(storeid);
                condition = x => x.orderStatus == 1 && x.orderTime <= todayNow && x.orderTime >= midnight && x.storeid == storeID;
            }
            if (date1 != null && date1 != "" && date2 != null && date2 != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
            }
            if (date1 != "" && date2 != "" && storeid != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                int storeID = Convert.ToInt32(storeid);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID;
            }
            var listOrderCh = bll.Search().Where(condition).GroupBy(x => x.orderPay).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, orderPay = x.FirstOrDefault().orderPay == 1 ? "支付宝" : x.FirstOrDefault().orderPay == 2 ? "微信" : x.FirstOrDefault().orderPay == 3 ? "优惠券" : "人脸", }).ToList();
            foreach(var item in listOrderCh )
            {
                ModeOfPay model = new ModeOfPay();
                model.payType = item.orderPay;
                model.money = item.orderSumprice;
                listt.Add(model);
            }

             return Json(listt, JsonRequestBehavior.AllowGet);
        }
        
    }
}
